<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Game\EventList;
use App\Models\Game\GameChannel;
use App\Models\Game\GamePackage;
use App\Models\Game\HotVersion;
use App\Models\Game\ServerSign;
use App\Services\RedisService;
use App\Utils\MyLog;
use App\Utils\ResponseUtil;

class GameSignController extends Controller
{
    private RedisService $RedisService;

    /**
     * ServerController constructor.
     * @param RedisService $RedisService
     */
    public function __construct(RedisService $RedisService)
    {
        $this->RedisService = $RedisService;
    }

    public function paginate()
    {
        $model = ServerSign::query();
        $page = request()->get('page',1);
        $pageSize = request()->get('limit',20);
        $paginate = $model->orderBy('id')->paginate($pageSize,'*','page',$page);
        $data = $paginate->items();
        $res = [];
        $res['code'] = 0;
        $res['count'] = $paginate->total();
        $res['data'] = $data;
        return ResponseUtil::json($res);
    }

    public function add(){
        $data = request()->all();
        MyLog::info('111',json_encode($data));
        $packageNo = data_get($data,'packages', []);
        $packageIds = data_get($data,'packageIds');
        $channelIds = data_get($data,'channelIds');
        $qddescription = data_get($data,'qddescription');
        $isSign = data_get($data,'isSign');
        $bind = data_get($data,'bind');
        $isPay = data_get($data,'isPay');

        $qdweixinbg = data_get($data,'qdweixinbg');
        $qdweixin = data_get($data,'qdweixin');
        $rewardlist = data_get($data,'rewardlist');
        MyLog::info('packageNo ',json_encode($packageNo ));
        MyLog::info('ackageIds ',json_encode($packageIds ));
        MyLog::info('channelIds ',json_encode($channelIds ));

        $datarow=[];

        $packagenNos = implode(',', array_column($packageNo, 'package_name'));
        $packageIdsString = implode(',', $packageIds);
        $channelIdsString = implode(',', $channelIds);
        $datarow['package_no']=$packageIdsString;
        $datarow['package_name']=$packagenNos;
        $datarow['channel_id']=$channelIdsString;
        $datarow['qdweixinbg']=$qdweixinbg;
        $datarow['qdweixin']=$qdweixin;
        $datarow['qddescription']=$qddescription;
        $datarow['isSign']=$isSign;
        $datarow['bind']=$bind;
        $datarow['isPay']=$isPay;
        $datarow['rewardlist']=$rewardlist;
        $datarow['created_at'] = date("Y-m-d H:i:s");

        ServerSign::query()->insert($datarow);
        return ResponseUtil::success();
    }

    public function detail($id){
        $data = ServerSign::query()->where('id',$id)->first();

        MyLog::info('Signs'.json_encode($data));
        $pno = [$data->package_no];
        $pnos = implode(',', $pno);
        $pnosArray = explode(',', $pnos);
        $cno = [$data->channel_id];
        $cnos = implode(',', $cno);
        $cnosArray = explode(',', $cnos);

        //根据包号查询包号基本信息
        $gamepackage = GamePackage::query()->whereIn('id',$pnosArray)->get();
        $gameChannel = GameChannel::query()->whereIn('id',$cnosArray)->get();

        MyLog::info('gamepackage'.json_encode($gamepackage));

        $data['packages']=$gamepackage;
        $data['channels']=$gameChannel;
        return ResponseUtil::success($data);
    }

    public function edit($id){
        $data = request()->all();
        MyLog::info('111',json_encode($data));
        $packageNo = data_get($data,'packages', []);
        $packageIds = data_get($data,'packageIds');
        $channelIds = data_get($data,'channelIds');
        $qddescription = data_get($data,'qddescription');
        $isSign = data_get($data,'isSign');
        $qdweixinbg = data_get($data,'qdweixinbg');
        $qdweixin = data_get($data,'qdweixin');
        $rewardlist = data_get($data,'rewardlist');
        $bind = data_get($data,'bind');
        $isPay = data_get($data,'isPay');


        $datarow=[];

        $packagenNos = implode(',', array_column($packageNo, 'package_name'));
        $packageIdsString = implode(',', $packageIds);
        $channelIdsString = implode(',', $channelIds);
        $datarow['package_no']=$packageIdsString;
        $datarow['package_name']=$packagenNos;
        $datarow['channel_id']=$channelIdsString;
        $datarow['qdweixinbg']=$qdweixinbg;
        $datarow['qdweixin']=$qdweixin;
        $datarow['qddescription']=$qddescription;
        $datarow['isSign']=$isSign;
        $datarow['rewardlist']=$rewardlist;
        $datarow['bind']=$bind;
        $datarow['isPay']=$isPay;


        $datarow['updated_at'] = date("Y-m-d H:i:s");


        $b = ServerSign::query()->where('id',$id)->update($datarow);
        if ($b){
            return ResponseUtil::success();
        } else {
            return ResponseUtil::error();
        }
    }

    public function delete($id){
        ServerSign::query()->where('id',$id)->delete();
        return ResponseUtil::success();
    }


    private function showCharTime($seconds){
        $minute = floor($seconds / 60);
        $second = $seconds % 60;
        return "{$minute}分{$second}秒";
    }


}
